#include "reservecheck.h"
#include "ui_reservecheck.h"
#include "reservehome.h"
#include "home.h"
#include <QListWidgetItem>
#include <QDebug>
#include "QMouseEvent"
reservecheck::reservecheck(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::reservecheck)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::FramelessWindowHint); // 设置窗口无边框
    // 创建一个QListWidget对象
    QListWidget *listWidget = new QListWidget();
    listWidget->setStyleSheet(
        "QListWidget {"
        "background-color: white;" // 设置 QListWidget 的背景颜色
        "border: 1px solid black;" // 设置 QListWidget 的边框
        "}"
        "QListWidget::item {"
        "padding: 5px;" // 设置项的内边距
        "background-color: gray;" // 设置项的背景颜色为灰色
        "border: 1px solid white;" // 设置项的边框，与背景颜色协调
        "color: black;" // 设置文本颜色，确保在灰色背景上的可读性
        "}"
        "QListWidget::item:hover {" // 设置鼠标悬停时的样式
        "background-color: darkgray;" // 悬停时背景颜色为深灰色
        "color: white;" // 悬停时文本颜色为白色，以增强对比度
        "}"
    );
    if(reserveHome::historyNum == 0){
        QString itemText = "没有预约信息";
        QListWidgetItem *item = new QListWidgetItem(itemText);
        listWidget->addItem(item);
    }
    // 遍历字符串列表并插入到QListWidget
    for (int i = reserveHome::historyNum - 1; i >= 0; --i) {
        QStringList list = reserveHome::history[i].split(" ");
        if(list.at(0).at(0)=="位"){
        // 构建富文本格式的字符串
        QString itemText;
        itemText += list.at(0) + "\n" + list.at(1)
                + " " + list.at(2) + "\n"
                + list.at(3) + " " + list.at(4);
        QListWidgetItem *item = new QListWidgetItem(itemText);
        listWidget->addItem(item);
        }else if(list.at(0).at(0)=="取"){
            QString itemText;
            itemText += list.at(0) + "\n" + list.at(1)
                    + "\n" + list.at(2);
            QListWidgetItem *item = new QListWidgetItem(itemText);
            listWidget->addItem(item);
        }else if(list.at(0).at(0)=="进"){
            QString itemText;
            itemText += list.at(0) + "\n" + list.at(1);
            QListWidgetItem *item = new QListWidgetItem(itemText);
            listWidget->addItem(item);
        }else if(list.at(0).at(0)=="离"){
            QString itemText;
            itemText += list.at(0) + "\n" + list.at(1);
            QListWidgetItem *item = new QListWidgetItem(itemText);
            listWidget->addItem(item);
        }else if(list.at(0).at(0)=="违"){
            QString itemText;
            itemText += list.at(0) + "\n" + list.at(1);
            QListWidgetItem *item = new QListWidgetItem(itemText);
            listWidget->addItem(item);
        }else if(list.at(0).at(0)=="禁"){
            QString itemText;
            itemText += list.at(0) + "\n" + list.at(1);
            QListWidgetItem *item = new QListWidgetItem(itemText);
            listWidget->addItem(item);
        }
    }
    ui->verticalLayout->addWidget(listWidget);
}

reservecheck::~reservecheck()
{
    delete ui;
}

void reservecheck::mousePressEvent(QMouseEvent *event) {
    if (event->button() == Qt::LeftButton) {
                initialMousePosition = event->globalPos(); // 记录鼠标按下时的全局位置
                isDragging = true; // 开始拖动
            }
    }

void reservecheck::mouseMoveEvent(QMouseEvent *event){
    if (isDragging && event->buttons() & Qt::LeftButton) {
                QPoint delta = event->globalPos() - initialMousePosition; // 计算鼠标移动的距离
                move(x() + delta.x(), y() + delta.y()); // 根据鼠标移动的距离来移动窗口
                initialMousePosition = event->globalPos(); // 更新鼠标位置
            }
    }
void reservecheck::mouseReleaseEvent(QMouseEvent *event) {
        if (event->button() == Qt::LeftButton) {
            isDragging = false; // 结束拖动
        }
    }


void reservecheck::on_pushButton_2_clicked()
{
    this->close();
    home *r = new home;
    r->show();
}
